#!/bin/bash
#
# 把要添加的域名写入文件 new-domain.list 中,
# 一个域名占一行.
#
# 2015-8-1


Ndomain=new-domain.list
Destdef=/etc/named/all.adding.def
#Destdef=all.adding.def
#Destdef=/etc/named/all.def

add_masterdomain(){
cat << HERE-DOCUMENT >> $Destdef
zone "$1" IN {
type master;
file "/etc/named/zone/all.zone";
allow-transfer { 172.20.9.8; };
};
HERE-DOCUMENT
}

add_slavedomain(){
cat << HERE2 >> $Destdef
zone "$1" IN {
type slave;
file "/var/named/slaves/$1";
masters { 172.20.9.9; };
};
HERE2
}

sed -i '/^$/d' $Ndomain

while read field1 _; do
    echo $field1
    if grep -q $field1 /etc/named/all.*def; then
        echo "you already have add this domain !!!"
    else
        ## 判断是 master 还是 slave
        if grep -q "type master" /etc/named/all.def; then
            echo "Now adding master config ..."
            add_masterdomain $field1
        else
            echo "Now adding slave config ..."
            add_slavedomain $field1
        fi
    fi
done < $Ndomain